package com.fss.user.dao;
import com.fssDataModels.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class UserDaoImpl implements UserDao{
	private SessionFactory _sessions;
	
	public UserDaoImpl()
	{
		// Create the SessionFactory from hibernate.cfg.xml
		_sessions = new Configuration()				
				.configure()
				.buildSessionFactory();
	}
	//CRUD functions
	public TbUser getUser(String userId)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		TbUser result = null;
		try {
			tx = session.beginTransaction();
			String hql=	"select user " +
						"from TbUser as user " +
						"where user.userId=:userId ";		
			
			Query query = session.createQuery(hql);
			query.setParameter("userId", userId);
			
			if(!query.list().isEmpty())				
				result=(TbUser)query.list().get(0);
			
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	//return: - 0 --> failed
	//		  - 1 --> success
	public int insertUser(TbUser user)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.persist(user);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	//return: - 0 --> failed
	//		  - 1 --> success
	public int updateUser(TbUser user)
	{
		Session session = _sessions.openSession();
		int result=1;
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			session.update(user);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	//function: deleteActivity
	//return: - 0 --> failed
	//		  - 1 --> success
	public int deleteUser(TbUser user)
	{
		int success=0;
		Session session = _sessions.openSession();
		String userId= user.getUserId();
		try {		    
		      String hql = "delete from TbUser user where user.userId = :userId";
		      Query query = session.createQuery(hql);
		      query.setParameter("userId", userId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=0;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
	
	public boolean checkExist(TbUser user)
	{
		TbUser result = getUser(user.getUserId());
		if(result!=null)
			return true;
		else
			return false;
	}
	
	//contact related functions
	public List getContacts(TbUser user)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		List result = null;
		String userId = user.getUserId();
		
		try {
			tx = session.beginTransaction();
			String hql=	"select  contact.tbUserByFriendId " +
						"from TbFriend as contact " +
						"where contact.tbUserByUserId.userId =:userId ";		
			
			Query query = session.createQuery(hql);
			query.setParameter("userId", userId);
			
			if(!query.list().isEmpty())				
				result= query.list();
			
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	public int addContact  (TbUser user,TbUser friend)throws HibernateException
	{
		TbFriendId contactId = new TbFriendId(user.getUserId(),friend.getUserId());
		TbFriend contact = new TbFriend(contactId,user,friend);
		
		
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.persist(contact);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
			throw he;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	public int deleteContact(TbUser user,TbUser friend)
	{
		TbFriendId contactId = new TbFriendId(user.getUserId(),friend.getUserId());
		
		int success=0;
		Session session = _sessions.openSession();
		
		try {		    
		      String hql = "delete from TbFriend contact where contact.id = :contactId";
		      Query query = session.createQuery(hql);
		      query.setParameter("contactId", contactId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=0;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
	
	

}
